package me.caofeng.service;

import me.caofeng.enums.ResponseCode;
import me.caofeng.pojo.Response;
import me.caofeng.service.api.IDataBindService;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.Optional;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.service
 * @Description:
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-22 16:46
 * @Version: V1.0
 */
@Service
public class DataBindService implements IDataBindService {

    @Override
    public Response<String> verifyData(BindingResult result) {
        Optional<ObjectError> error = result.getAllErrors()
                .stream().filter(objectError -> null != objectError).findFirst();

        if (error.isPresent()) {
            return Response.response(ResponseCode.ILLEGAL_ARGUMENT, error.get().getDefaultMessage(), null);
        }
        return Response.response(ResponseCode.SUCCESS,"参数正确",null);
    }
}
